<?php

namespace app\models;

use app\components\BaseActiveRecord;
use Yii;

/**
 * This is the model class for table "{{%categories}}".
 *
 * @property int $id
 * @property int $parent_id
 * @property int $type
 * @property string $title
 * @property string $description
 * @property string $slug
 * @property int $count
 * @property int $sort
 */
class Categories extends BaseActiveRecord {

    const TYPE_POST = 1;
    const TYPE_PAGE = 2;
    const TYPE_VIDEO = 3;

    public static $typeViews = [
        self::TYPE_POST => 'post',
        self::TYPE_PAGE => 'page',
        self::TYPE_VIDEO => 'video',
    ];

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%categories}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['title'], 'required'],
            [['slug'], 'unique', 'skipOnEmpty' => TRUE],
            [['type'], 'in', [self::TYPE_POST, self::TYPE_VIDEO, self::TYPE_PAGE]],
            [['parent_id', 'type', 'count', 'sort'], 'integer'],
            [['title', 'description'], 'string', 'max' => 200],
            [['slug'], 'string', 'max' => 100],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'parent_id' => 'Parent ID',
            'type' => 'Type',
            'title' => 'Title',
            'description' => 'Description',
            'slug' => 'Slug',
            'count' => 'Count',
            'sort' => 'Sort',
        ];
    }

    public function access() {
        return $this->cache(function () {
            return static::find()->indexBy('id')->asArray()->all();
        });
    }

    public function getViewName() {
        return $this->_statusName(self::$typeViews, 'type');
    }

}
